package com.genersoft.iot.vmp.conf.quartz;

import com.genersoft.iot.vmp.storager.dao.VedioToRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private VedioToRecord vedioToRecord;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取过期的Key
        String expiredKey = message.toString();

        // 处理Key过期事件
        System.out.println("Key expired: " + expiredKey);

        // 在这里执行您想要的业务逻辑
        if("vedio".equals(expiredKey)){
               restTemplate.getForObject("http://8.130.78.223:18080/api/play/stop/34020000001320000001/34020000001320000001"
               ,String.class);

            vedioToRecord.change();
            log.info("录像关闭...");


        }
    }
}
